package leetcode.sixeighttwo;


import java.util.Stack;

/**
 * Description:
 * Created By xxm
 */
public class Solution {
    public static int calPoints(String[] ops) {
        Stack<Integer> points = new Stack<>(); // store point
        int res = 0; // init total count

        for (String top : ops) {
            if (top.equals("+")) {
                int last = points.pop();
                int temp = last + points.peek();
                points.push(last);
                points.push(temp);
            } else if (top.equals("C")) {
                points.pop();
            } else if (top.equals("D")) {
                points.push(points.peek() * 2);
            } else {
                points.push(Integer.valueOf(top));
            }
        }

        for (Integer point : points) {
            res = point + res;
        }

        return res;
    }

    public static void main(String[] args) {

        // “5”，“2”，“C”，“D”，“+”
        String[] ops = new String[]{"5", "2", "C", "D", "+"};

        // “5”，“ -  2”，“4”，“C”，“D”，“9”，“+”，“+”
//        String[] ops = new String[]{"5", "-2", "4", "C", "D", "9", "+", "+"};

        System.out.println(calPoints(ops));
    }
}